import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, LoadingController } from 'ionic-angular';
import { Utils } from '../../common/utils';
import { UiserviceProvider } from '../../providers/uiservice/uiservice';
import { BusiserviceProvider } from '../../providers/busiservice/busiservice';
import { LoadingUtil } from '../../common/loading';

/**
 * Generated class for the MycardPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-mycard',
  templateUrl: 'mycard.html',
})
export class MycardPage {
  mycard: Card;
  loading:LoadingUtil;
  constructor(public navCtrl: NavController,
    private loadingCtrl: LoadingController, public navParams: NavParams, public busi: BusiserviceProvider, private uiservice: UiserviceProvider) {
    this.mycard = this.navParams.get("mycard");
    this.loading = new LoadingUtil(this.loadingCtrl);
    if (this.mycard == null) {
      this.mycard = new Card();
    }
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad MycardPage');
  }
  savemycard() {
    if (Utils.isNullOrEmpty(this.mycard.name)) {
      alert("名字为空!");
      return;
    }
    if (Utils.isNullOrEmpty(this.mycard.nj)) {
      alert("年级为空!");
      return;
    }

    if (Utils.isNullOrEmpty(this.mycard.zy)) {
      alert("专业为空!");
      return;
    }

    if (Utils.isNullOrEmpty(this.mycard.email)) {
      alert("邮箱为空!");
      return;
    }
    if (Utils.isNullOrEmpty(this.mycard.des)) {
      alert("个人描述为空!");
      return;
    }
    this.loading.showLoading();
    this.busi.saveMyCard(this.mycard).subscribe(
      data => {
        this.loading.hideLoading();
        if (data._success) {
          this.uiservice.showToast("保存成功!");
          this.navCtrl.pop();
        } else {
          this.uiservice.showAlert(data._message);
        }
      },
      error => {
        this.loading.hideLoading();
        this.uiservice.showAlert("网络异常!")
      }
    );
  }
}
class Card {
  name: string = "";
  nj: string = "";
  zy: string = "";
  email: string = "";
  phone: string = "";
  des: string = "";
}
